package com.kpmg.datalake.common.service.impl.job;

import com.kpmg.datalake.common.enums.ProjectSyncStatusEnum;
import com.kpmg.datalake.common.service.IProjectService;
import com.kpmg.datalake.db.model.Project;
import com.kpmg.datalake.schedule.model.ScheduleJob;
import com.kpmg.datalake.schedule.service.ScheduleExecuteService;
import java.util.List;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;

/**
 * @author Alex.zhao [alex.zhao@kpmg.com]
 * @since Created on azhao6 on 2018-08-27 21:45:12
 */
@Service
public class ProjectDataSyncImpl implements ScheduleExecuteService {

  private static final Logger logger = LoggerFactory.getLogger(ProjectDataSyncImpl.class);

  @Resource
  private IProjectService projectService;

  @Resource
  private TaskExecutor taskExecutor;

  /**
   * 动态任务实现的接口
   *
   * @param scheduleJob 任务信息
   */
  @Override
  public void execute(ScheduleJob scheduleJob) {
    //获取未同步完成项目
    List<Project> syncNotCompleteProjectList = projectService.getSyncNotCompleteProjectList(
        ProjectSyncStatusEnum.UN_SYNC);
    for (Project project : syncNotCompleteProjectList) {
      taskExecutor.execute(() -> {
        try {
          projectService.syncProjectData(project.getPrjId());
        } catch (Exception e) {
          logger.error(e.getMessage(), e);
        }
      });


    }
  }


}
